// Pipeline Component Library Example
// ----------------------------------
// Colin Bowern (colinbowern@hotmail.com)
// January 2007
//
// Copyright (c) 2007 Colin Bowern
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// * The above copyright notice and this permission notice shall be included in
//   all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#region References
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
#endregion

namespace RockstarGuys.PipelineComponents
{
    /// <summary>
    /// Provides a user interface for the Calculate Tax component properties.
    /// </summary>
    [ComVisible(true), Guid("E91F9DE5-5098-494A-9D73-9D33125E12AF"), ProgId("RSG.CalculateTaxProperties")]
    public partial class CalculateTaxProperties : Form, Microsoft.CommerceServer.Interop.IPipelineComponentUI
    {
        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="CalculateTaxProperties"/> class.
        /// </summary>
        public CalculateTaxProperties()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Shows the properties.
        /// </summary>
        /// <param name="pdispComponent">The pdisp component.</param>
        public void ShowProperties(object pdispComponent)
        {
            CalculateTax calculateTax = (CalculateTax)pdispComponent;

            this.EnableTaxExemptionRules.Checked = calculateTax.EnableTaxExemptionRules;
            this.CountryTaxExemptionKey.Text = calculateTax.CountryTaxExemptionKey;
            this.CountryTaxExemptionKeySource.Text = calculateTax.CountryTaxExemptionKeySource.ToString();
            this.RegionTaxExemptionKey.Text = calculateTax.RegionTaxExemptionKey;
            this.RegionTaxExemptionKeySource.Text = calculateTax.RegionTaxExemptionKeySource.ToString();

            EnableTaxExemptionRules_CheckedChanged(null, EventArgs.Empty);

            DialogResult result = this.ShowDialog();
            if (result == DialogResult.OK)
            {
                calculateTax.EnableTaxExemptionRules = this.EnableTaxExemptionRules.Checked;
                calculateTax.CountryTaxExemptionKey = this.CountryTaxExemptionKey.Text;
                calculateTax.CountryTaxExemptionKeySource = (CalculateTax.KeySources)Enum.Parse(typeof(CalculateTax.KeySources), this.CountryTaxExemptionKeySource.Text, true);
                calculateTax.RegionTaxExemptionKey = this.RegionTaxExemptionKey.Text;
                calculateTax.RegionTaxExemptionKeySource = (CalculateTax.KeySources)Enum.Parse(typeof(CalculateTax.KeySources), this.RegionTaxExemptionKeySource.Text, true);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the EnableTaxExemptionRules control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void EnableTaxExemptionRules_CheckedChanged(object sender, EventArgs e)
        {
            CountryTaxExemptionKey.Enabled = EnableTaxExemptionRules.Checked;
            CountryTaxExemptionKeySource.Enabled = EnableTaxExemptionRules.Checked;
            RegionTaxExemptionKey.Enabled = EnableTaxExemptionRules.Checked;
            RegionTaxExemptionKeySource.Enabled = EnableTaxExemptionRules.Checked;
        }

        /// <summary>
        /// Handles the Click event of the Submit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Submit_Click(object sender, EventArgs e)
        {
            if (this.EnableTaxExemptionRules.Checked)
            {
                if (string.IsNullOrEmpty(this.CountryTaxExemptionKey.Text))
                {
                    this.CountryTaxExemptionKeyErrorProvider.SetError(this.CountryTaxExemptionKey, StringResources.CountryTaxExemptionKeyRequired);
                    return;
                }
                if (string.IsNullOrEmpty(this.CountryTaxExemptionKeySource.Text))
                {
                    this.CountryTaxExemptionKeyErrorProvider.SetError(this.CountryTaxExemptionKey, StringResources.CountryTaxExemptionKeySourceRequired);
                    return;
                }
                if (string.IsNullOrEmpty(this.RegionTaxExemptionKey.Text))
                {
                    this.RegionTaxExemptionKeyErrorProvider.SetError(this.RegionTaxExemptionKey, StringResources.RegionTaxExemptionKeyRequired);
                    return;
                }
                if (string.IsNullOrEmpty(this.RegionTaxExemptionKeySource.Text))
                {
                    this.RegionTaxExemptionKeyErrorProvider.SetError(this.RegionTaxExemptionKey, StringResources.RegionTaxExemptionKeySourceRequired);
                    return;
                }

                this.Close();
            }
        }
        #endregion
    }
}